
#include "MyFrameListener.h"

MyFrameListener::MyFrameListener(Ogre::SceneNode* node, Ogre::Entity* ent, RenderWindow* win, Ogre::Camera* cam) : _node(node), _ent(ent), _Cam(cam), _PolyMode(Ogre::PolygonMode::PM_SOLID) {

	_movementspeed = 100;
	_WalkingSpeed = 50.0f;
	_rotation = 0.0f;

	_timer.reset();

	_aniState = _ent->getAnimationState("RunBase");
	_aniState->setLoop(false);

	_aniStateTop = _ent->getAnimationState("RunTop");
	_aniStateTop->setLoop(false);

	size_t windowHnd = 0;
	std::stringstream windowHndStr;
	win->getCustomAttribute("WINDOW", &windowHnd);
	windowHndStr << windowHnd;

	OIS::ParamList pl;
	pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
	_man = OIS::InputManager::createInputSystem(pl);
	_key = static_cast<OIS::Keyboard*>(_man->createInputObject(OIS::OISKeyboard, false));
	_mouse = static_cast<OIS::Mouse*>(_man->createInputObject(OIS::OISMouse, false));
}

MyFrameListener::~MyFrameListener() {

	_man->destroyInputObject(_key);
	_man->destroyInputObject(_mouse);

	OIS::InputManager::destroyInputSystem(_man);
}


bool MyFrameListener::frameStarted(const Ogre::FrameEvent &evt) {

	bool walked = false;
	Ogre::Vector3 SinbadTranslate(0,0,0);
	Ogre::Vector3 translate(0,0,0);
		
	_key->capture();
	_mouse->capture();

	if(_key->isKeyDown(OIS::KC_ESCAPE)) { return false; }
	if(_key->isKeyDown(OIS::KC_UP)) {

		SinbadTranslate += Ogre::Vector3(0,0,-1);
		_rotation = 3.14f;
		walked = true;
	}
	if(_key->isKeyDown(OIS::KC_DOWN)) {

		SinbadTranslate += Ogre::Vector3(0,0,1);
		_rotation = 0.0f;
		walked = true;
	}
	if(_key->isKeyDown(OIS::KC_LEFT)) {

		SinbadTranslate += Ogre::Vector3(-1,0,0);
		_rotation = -1.57f;
		walked = true;
	}
	if(_key->isKeyDown(OIS::KC_RIGHT)) {

		SinbadTranslate += Ogre::Vector3(1,0,0);
		_rotation = 1.57f;
		walked = true;
	}
	if(_key->isKeyDown(OIS::KC_W)) {

		translate += Ogre::Vector3(0,0,-1);
	}
	if(_key->isKeyDown(OIS::KC_S)) {

		translate += Ogre::Vector3(0,0,1);
	}
	if(_key->isKeyDown(OIS::KC_A)) {

		translate += Ogre::Vector3(-1,0,0);
	}
	if(_key->isKeyDown(OIS::KC_D)) {

		translate += Ogre::Vector3(1,0,0);
	}
	if(_key->isKeyDown(OIS::KC_R) && _timer.getMilliseconds() > 250) {

		if(_PolyMode == PM_SOLID) _PolyMode = Ogre::PolygonMode::PM_WIREFRAME;
		else if(_PolyMode == PM_WIREFRAME) _PolyMode = Ogre::PolygonMode::PM_POINTS;
		else if(_PolyMode == PM_POINTS) _PolyMode = Ogre::PolygonMode::PM_SOLID;

		_Cam->setPolygonMode(_PolyMode);
		_timer.reset();
	}

	float rotX = _mouse->getMouseState().X.rel * evt.timeSinceLastFrame * -1;
	float rotY = _mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;

	_Cam->yaw(Ogre::Radian(rotX));
	_Cam->pitch(Ogre::Radian(rotY));
	_Cam->moveRelative(translate * evt.timeSinceLastFrame * _movementspeed);

	//_node->translate(translate * _movementspeed * evt.timeSinceLastFrame);

	if(walked) {

		_aniState->setEnabled(true);
		_aniStateTop->setEnabled(true);

		if(_aniState->hasEnded()) {

			_aniState->setTimePosition(0.0f);
		}
		if(_aniStateTop->hasEnded()) {

			_aniStateTop->setTimePosition(0.0f);
		}
	}
	else {

		_aniState->setTimePosition(0.0f);
		_aniState->setEnabled(false);
		_aniStateTop->setTimePosition(0.0f);
		_aniStateTop->setEnabled(false);
	}

	_node->translate(SinbadTranslate * _WalkingSpeed * evt.timeSinceLastFrame);
	_node->resetOrientation();
	_node->yaw(Ogre::Radian(_rotation));

	_aniState->addTime(evt.timeSinceLastFrame);
	_aniStateTop->addTime(evt.timeSinceLastFrame);

	return true;
}